﻿<UserControl x:Class="Songhay.BiggestBox.Desktop.Modules.Validation.Views.BasicValidationView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d"
    xmlns:rxSharedModels="clr-namespace:Songhay.BiggestBox.Portable.Shared.Models;assembly=Songhay.BiggestBox.Portable.Shared"
    xmlns:vc="clr-namespace:Songhay.ValueConverters;assembly=Songhay.Mvvm"
    d:DesignWidth="{x:Static rxSharedModels:AppScalars.ModuleWidth}" d:DesignHeight="{x:Static rxSharedModels:AppScalars.ModuleHeight}">
    <UserControl.Resources>
        <vc:BooleanToTextConverter x:Key="BooleanToTextConverter" />

        <ControlTemplate x:Key="ClassicErrorTemplate">
            <DockPanel>
                <!-- FontAwesome &#xf06a; -->
                <Path DockPanel.Dock="Right"
                    Data="M768 1408q209 0 385.5 -103t279.5 -279.5t103 -385.5t-103 -385.5t-279.5 -279.5t-385.5 -103t-385.5 103t-279.5 279.5t-103 385.5t103 385.5t279.5 279.5t385.5 103zM896 161v190q0 14 -9 23.5t-22 9.5h-192q-13 0 -23 -10t-10 -23v-190q0 -13 10 -23t23 -10h192 q13 0 22 9.5t9 23.5zM894 505l18 621q0 12 -10 18q-10 8 -24 8h-220q-14 0 -24 -8q-10 -6 -10 -18l17 -621q0 -10 10 -17.5t24 -7.5h185q14 0 23.5 7.5t10.5 17.5z"
                    Fill="Red"
                    Margin="4 0 0 0"
                    Stretch="UniformToFill"
                    VerticalAlignment="Center"
                    Width="24" Height="24"
                />
                <Border BorderBrush="Red" BorderThickness="2" CornerRadius="2">
                    <AdornedElementPlaceholder />
                </Border>
            </DockPanel>
        </ControlTemplate>

        <Style x:Key="StackDataForm" TargetType="{x:Type StackPanel}">
            <Setter Property="StackPanel.Orientation" Value="Horizontal" />
            <Setter Property="FrameworkElement.Margin" Value="16" />
        </Style>
        <Style x:Key="StackLabels" TargetType="{x:Type StackPanel}">
            <Setter Property="FrameworkElement.Width" Value="90" />
        </Style>
        <Style x:Key="StackFields" TargetType="{x:Type StackPanel}">
            <Setter Property="FrameworkElement.Width" Value="320" />
        </Style>

        <Style x:Key="ValidationErrorStyle" TargetType="{x:Type FrameworkElement}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors)[0].ErrorContent,  RelativeSource={RelativeSource Self}}" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type Label}">
            <Setter Property="Height" Value="32" />
            <Setter Property="Margin" Value="0,0,0,8" />
        </Style>
        <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource ValidationErrorStyle}">
            <Setter Property="Height" Value="32" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="Margin" Value="0,0,0,8" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Width" Value="90" />
        </Style>
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource ValidationErrorStyle}">
            <Setter Property="Height" Value="32" />
            <Setter Property="Margin" Value="0,0,0,8" />
        </Style>
    </UserControl.Resources>
    <StackPanel Style="{StaticResource StackDataForm}">
        <StackPanel Style="{StaticResource StackLabels}" Width="96">

            <Label
                Content="_First Name"
                Target="{Binding ElementName=FirstName}"
                />

            <Label
                Content="_Last Name"
                Target="{Binding ElementName=LastName}"
                />

            <Label
                Content="E_mail"
                Target="{Binding ElementName=Email}"
                />

            <Label
                Content="U.S. _State"
                Target="{Binding ElementName=USState}"
                />

        </StackPanel>
        <StackPanel x:Name="FormFields" Style="{StaticResource StackFields}" Width="480">

            <TextBox Name="FirstName" Validation.ErrorTemplate="{StaticResource ClassicErrorTemplate}"
                Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=Explicit, ValidatesOnNotifyDataErrors=True}"
                />

            <TextBox Name="LastName" Validation.ErrorTemplate="{StaticResource ClassicErrorTemplate}"
                Text="{Binding Path=LastName, Mode=TwoWay, UpdateSourceTrigger=Explicit, ValidatesOnNotifyDataErrors=True}"
                />

            <TextBox Name="Email" Validation.ErrorTemplate="{StaticResource ClassicErrorTemplate}"
                Text="{Binding Path=Email, Mode=TwoWay, UpdateSourceTrigger=Explicit, ValidatesOnNotifyDataErrors=True}"
                />

            <StackPanel Orientation="Horizontal">

                <ComboBox Name="USState" Validation.ErrorTemplate="{StaticResource ClassicErrorTemplate}"
                    ItemsSource="{Binding Path=SelectedUSStates}"
                    SelectedItem="{Binding Path=SelectedUSState, Mode=TwoWay, UpdateSourceTrigger=Explicit, ValidatesOnNotifyDataErrors=True}"
                    />

                <Label Content="In U.S.?"
                    Margin="32,0,16,0"
                    Target="{Binding ElementName=ResidingInUS, Mode=OneWay, UpdateSourceTrigger=Explicit, ValidatesOnNotifyDataErrors=True}"
                    />

                <ComboBox Name="ResidingInUS" Validation.ErrorTemplate="{StaticResource ClassicErrorTemplate}"
                    ItemsSource="{Binding Path=YesNoValues}"
                    SelectedItem="{Binding IsResidingInUS, Converter={StaticResource BooleanToTextConverter}, Mode=TwoWay, UpdateSourceTrigger=Explicit, ValidatesOnNotifyDataErrors=True}"
                    />
            </StackPanel>

            <StackPanel>
                <Button x:Name="Validate"
                    Content="Validate!"
                    Cursor="Hand" HorizontalAlignment="Right"
                    Margin="0,16,0,0"
                    Width="96"
                    />
            </StackPanel>
        </StackPanel>
    </StackPanel>
</UserControl>
